package com.grj.park.boot.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.grj.park.boot.domain.UserRole;
import com.grj.park.boot.service.UserRoleService;
import com.grj.park.boot.mapper.UserRoleMapper;
import com.grj.park.boot.util.StringUtil;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author eren
* @description 针对表【user_role】的数据库操作Service实现
* @createDate 2024-10-23 16:11:53
*/
@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole>
    implements UserRoleService{

    @Override
    public Page<UserRole> fetchUserRolePage(int currentPage, int pageSize, UserRole userRole, List<Date> during) {
        QueryWrapper<UserRole> roleQueryWrapper = Wrappers.query();
        if (during != null && !during.isEmpty()) {
            roleQueryWrapper.between("create_time", during.get(0), during.get(1));
        }
        Map<String, Object> stringObjectMap = BeanUtil.beanToMap(userRole);
        HashMap<String, Object> map = new HashMap<>();
        stringObjectMap.forEach((k, v) -> {
            if (v != null) {
                map.put(StringUtil.toCamelCase(k), v);
            }
        });
        roleQueryWrapper.allEq(map, false);
        return baseMapper.selectPage(new Page<>(currentPage, pageSize), roleQueryWrapper);
    }



    @Override
    public void delUserRoles(List<UserRole> userRoles) {
        LambdaQueryWrapper<UserRole> lambdaQueryWrapper = Wrappers.lambdaQuery();
        userRoles.forEach(userRole -> lambdaQueryWrapper.or().eq(UserRole::getUserId, userRole.getUserId()));
        baseMapper.delete(lambdaQueryWrapper);
    }

    @Override
    public void updateUserRoles(List<UserRole> userRoles) {
        baseMapper.updateUserRole(userRoles);
    }

    @Override
    public List<UserRole> listAll(List<UserRole> userRoles) {
        return baseMapper.selectAll(userRoles);
    }
}




